package com.wy.fontchangecolor;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


    private String TAG = "MainActivity";

    private FontChangeColor fontChangeColor1;
    private FontChangeColor fontChangeColor2;
    private FontChangeColor fontChangeColor3;
    private ViewPager viewPager;
    private ArrayList<View> aList;
    private ArrayList<FontChangeColor> aFontChangeColorList;
    private MyPagerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fontChangeColor1 = findViewById(R.id.fontChangeColor1);
        fontChangeColor2 = findViewById(R.id.fontChangeColor2);
        fontChangeColor3 = findViewById(R.id.fontChangeColor3);
        viewPager = findViewById(R.id.viewPager);

        aFontChangeColorList = new ArrayList<>();
        aFontChangeColorList.add(fontChangeColor1);
        aFontChangeColorList.add(fontChangeColor2);
        aFontChangeColorList.add(fontChangeColor3);

        aList = new ArrayList<>();
        LayoutInflater li = getLayoutInflater();
        aList.add(li.inflate(R.layout.view_one, null, false));
        aList.add(li.inflate(R.layout.view_two, null, false));
        aList.add(li.inflate(R.layout.view_three, null, false));
        mAdapter = new MyPagerAdapter(aList);
        viewPager.setAdapter(mAdapter);

        //透明度动画
        ValueAnimator valueAnimator = ObjectAnimator.ofInt(0, 100);
        valueAnimator.setDuration(1);
        valueAnimator.start();
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int animatedValue = (int) animation.getAnimatedValue();
                fontChangeColor1.setChangeTextColor(1, animatedValue);
            }
        });

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.i(TAG, "position:" + position + ",positionOffset:" + positionOffset + ",positionOffsetPixels:" + positionOffsetPixels);

                try {

                    FontChangeColor left = aFontChangeColorList.get(position);
                    left.setChangeTextColor(2, (int) ((1 - positionOffset) * 100));

                    FontChangeColor right = aFontChangeColorList.get(position + 1);
                    right.setChangeTextColor(1, (int) (positionOffset * 100));

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onPageSelected(int position) {
                Log.i(TAG, "position:" + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
//                Log.i(TAG, "state:"+state);
            }

        });

    }

}